// 支付模型
const { Pay } = require('../../../models/payrecord');
// 订单模型
const { Orders } = require('../../../models/order');
// 商品模型
const { Goods } = require('../../../models/good');
// 统一数据返回类
const { DataReturn } = require('../../../common/returnclass');

module.exports = async (req, res) => {
	let { order_num, price,user_id, goods_id } = req.fields;
	console.log(req.fields);

	let pays = new Pay({
		order_num,
		price,
		user_id,
		is_pay: true
	});
	pays.save()
	.then(()=>{
		return order_num.forEach(async (item,index)=>{
			await Orders.updateOne({ order_number: item },{ is_pay: true, state: 1 });
			let { sales_volume } = await Goods.findOne({_id:goods_id[index]});
			await Goods.updateOne({_id:goods_id[index]},{ sales_volume: sales_volume + 1});
		})
	}).then(()=>{
		DataReturn.getData(res,200, [],'支付成功');
	}).catch((err)=>{
		DataReturn.getData(res,400, err,'支付失败');
	})
	

}